package com.jiangxiaofei.citycloud.mvp;

import android.content.Context;

import com.jiangxiaofei.citycloud.bean.FreshFruitCassifyBean;
import com.jiangxiaofei.citycloud.bean.FreshFruitHomeBean;
import com.jiangxiaofei.citycloud.bean.GoodDetailBean;
import com.jiangxiaofei.citycloud.bean.GoodIdBean;
import com.jiangxiaofei.citycloud.bean.GoodListBean;
import com.jiangxiaofei.citycloud.bean.NeedBean;
import com.jiangxiaofei.citycloud.httpUtil.HttpResult;
import com.jiangxiaofei.citycloud.httpUtil.OnLoadDataIm;
import com.jiangxiaofei.citycloud.httpUtil.RetrofitUtils;
import com.jiangxiaofei.citycloud.param.FleaGoodListParam;
import com.jiangxiaofei.citycloud.param.FleaNeedListParam;
import com.jiangxiaofei.citycloud.param.GoodsPublicParam;
import com.jiangxiaofei.citycloud.param.PutNeedParam;
import com.jiangxiaofei.citycloud.param.SearchParam;

import java.util.List;

import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author: JiangXFei
 * @date: 2017/11/21 0021
 * @content: 果蔬生鲜M
 */

public class FreshModel
        implements FreshConstant.GetFreshHomeModel, FreshConstant.GetFreshCassifyModel,
        FreshConstant.PutFreshGoodModel, FreshConstant.GetGoodDetailModel,
        FreshConstant.PutFreshNeedModel, FreshConstant.GetFreshNeedDetailModel,
        FreshConstant.GetFreshGoodListModel, FreshConstant.GetFreshNeedListModel,
        FreshConstant.SearchGoodListModel, FreshConstant.SearchNeedListModel {
    /**
     * 获取果蔬主页
     *
     * @param mContext
     * @param callback
     */
    @Override
    public void getFreshHome(Context mContext, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getFreshHome()
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<FreshFruitHomeBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<FreshFruitHomeBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });

    }

    /**
     * 获取果蔬全部分类
     *
     * @param mContext
     * @param callback
     */
    @Override
    public void getFreshCassify(Context mContext, int id, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getFreshAllCassify(id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<FreshFruitCassifyBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<FreshFruitCassifyBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 发布果蔬商品
     *
     * @param mContext
     * @param param
     * @param callbak
     */
    @Override
    public void PutFreshGood(Context mContext, GoodsPublicParam param, OnLoadDataIm callbak) {
        RetrofitUtils.getApiService(mContext)
                .putFreshGood(param.getTitle(), param.getDescrption(), param.getPrice(),
                        param.getCat(), param.getLat(), param.getLng(), param.getAuthor(),
                        param.getProarea(), param.getImg())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<GoodIdBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<GoodIdBean>> stringHttpResult) {
                        if (stringHttpResult.getCode() == 1) {
                            callbak.onSuccess(stringHttpResult.getData(), stringHttpResult.getMessage(), stringHttpResult.getCode());
                        } else {
                            callbak.onFailure(stringHttpResult.getMessage(), stringHttpResult.getCode());
                        }
                    }
                });
    }

    @Override
    public void getGoodDetail(Context mContext, int id, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getFreshGoodDetail(id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<GoodDetailBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<GoodDetailBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });


    }

    /**
     * 发布果蔬需求
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void putFreshNeed(Context mContext, PutNeedParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .putFreshNeed(param.getTitle(), param.getDescription(), param.getLat(),
                        param.getLng(), param.getPrice(), param.getProarea(),
                        param.getCat(), param.getAuthor())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<GoodIdBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<GoodIdBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 获取果蔬需求详情页
     *
     * @param mContext
     * @param id
     * @param callback
     */

    @Override
    public void getNeedDetail(Context mContext, int id, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getFreshNeedDetail(id)
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<NeedBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<NeedBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 获取商品列表
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void getGoodList(Context mContext, FleaGoodListParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getFreshGoodList(param.getId(), param.getPage(), param.getLat(),
                        param.getLng(), param.getIssue(), param.getPrice(), param.getDis())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<GoodListBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<GoodListBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 获取需求列表
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void getNeedList(Context mContext, FleaNeedListParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .getFreshNeedList(param.getId(), param.getPage(), param.getLat(),
                        param.getLng(), param.getIssue())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<NeedBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<NeedBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 搜索商品
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void searchGoods(Context mContext, SearchParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .searchFreshGoodList(param.getPage(), param.getKey(), param.getLat(), param.getLng())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<GoodListBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<GoodListBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }

    /**
     * 搜索需求
     *
     * @param mContext
     * @param param
     * @param callback
     */
    @Override
    public void searchNeeds(Context mContext, SearchParam param, OnLoadDataIm callback) {
        RetrofitUtils.getApiService(mContext)
                .searchFreshNeedList(param.getPage(), param.getKey(), param.getLat(), param.getLng())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<HttpResult<List<NeedBean>>>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(HttpResult<List<NeedBean>> listHttpResult) {
                        if (listHttpResult.getCode() == 1) {
                            callback.onSuccess(listHttpResult.getData(), listHttpResult.getMessage(), listHttpResult.getCode());
                        } else {
                            callback.onFailure(listHttpResult.getMessage(), listHttpResult.getCode());
                        }
                    }
                });
    }
}
